﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace StockMonitor
{
    /// <summary>
    /// 系统日志实体，从日志文件中加载，可用于显示
    /// </summary>
    /// <remarks>
    /// 代码编写：<author>LANCEZHANG-PC\zhangxl</author><br/>
    /// 编写时间：<datetime>2012-10-17 3:01 PM</datetime>
    /// </remarks>
    public class SystemLog
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="SystemLog"/> class.
        /// </summary>
        /// <param name="line">一行日志.</param>
        /// <remarks>
        /// 代码编写：<author>LANCEZHANG-PC\zhangxl</author><br/>
        /// 编写时间：<datetime>2012-10-17 3:01 PM</datetime>
        /// </remarks>
        public SystemLog(string line)
        {
            LogDateTime = Convert.ToDateTime(line.Substring(0, 19));
            Content = line.Substring(26).Trim();
        }

        /// <summary>
        /// 日志记录时间.
        /// </summary>
        /// <value>
        /// The log date time.
        /// </value>
        /// <remarks>
        /// 代码编写：<author>LANCEZHANG-PC\zhangxl</author><br/>
        /// 编写时间：<datetime>2012-10-17 3:01 PM</datetime>
        /// </remarks>
        public DateTime LogDateTime { get; set; }

        /// <summary>
        /// 日志日期.
        /// </summary>
        /// <remarks>
        /// 代码编写：<author>LANCEZHANG-PC\zhangxl</author><br/>
        /// 编写时间：<datetime>2012-10-17 3:02 PM</datetime>
        /// </remarks>
        public string LogDate
        {
            get
            {
                return LogDateTime.ToString("yyyy-MM-dd");
            }
        }

        /// <summary>
        /// 日志时间.
        /// </summary>
        /// <remarks>
        /// 代码编写：<author>LANCEZHANG-PC\zhangxl</author><br/>
        /// 编写时间：<datetime>2012-10-17 3:02 PM</datetime>
        /// </remarks>
        public string LogTime
        {
            get
            {
                return LogDateTime.ToString("hh:mm:ss");
            }
        }

        /// <summary>
        /// 日志内容.
        /// </summary>
        /// <value>
        /// The content.
        /// </value>
        /// <remarks>
        /// 代码编写：<author>LANCEZHANG-PC\zhangxl</author><br/>
        /// 编写时间：<datetime>2012-10-17 3:07 PM</datetime>
        /// </remarks>
        public string Content { get; set; }

        /// <summary>
        /// 根据起止时间查询系统日志.
        /// </summary>
        /// <param name="start">The start.</param>
        /// <param name="end">The end.</param>
        /// <returns></returns>
        /// <remarks>
        /// 代码编写：<author>LANCEZHANG-PC\zhangxl</author><br/>
        /// 编写时间：<datetime>2012-10-17 3:07 PM</datetime>
        /// </remarks>
        public static List<SystemLog> Query(DateTime start, DateTime end)
        {
            List<SystemLog> result = new List<SystemLog>();

            for (DateTime i = start; i <= end; i = i.AddDays(1))
            {
                string path = System.Windows.Forms.Application.StartupPath + @"\Logs\Info\" + i.ToString("yyyyMMdd");
                string header = i.ToString("yyyy-MM-dd");

                if (File.Exists(path))
                {
                    try
                    {
                        foreach (var line in File.ReadAllLines(path, Encoding.Default))
                        {
                            if (line.StartsWith(header))
                                result.Add(new SystemLog(line));
                        }
                    }
                    catch (Exception)
                    {
                        File.Copy(path, path + "temp", true);

                        foreach (var line in File.ReadAllLines(path + "temp", Encoding.Default))
                        {
                            if (line.StartsWith(header))
                                result.Add(new SystemLog(line));
                        }

                        File.Delete(path + "temp");
                    }
                }
            }

            return result;
        }
    }
}
